/**
 * help js
 * zhangjianbin@360.cn
 */

// placeholder
/*
var _isplaceholder = ('placeholder' in W('<input>')[0]);
if (!_isplaceholder) {
    W('.input').forEach(function (el) {
        el.value = W(el).attr('placeholder');
        if (!W(el).hasClass('input-placeholder')) {
            W(el).addClass('input-placeholder');
        }
    });

    W('.input')
        .on('focus', function (e) {
            var that = W(this);
            that.addClass('input-active');

            if (that.val() == that.attr('placeholder')) {
                that.val('');
            }
        })
        .on('blur', function (e) {
            var that = W(this);
            that.removeClass('input-active');
            if (that.val() == '') {
                that.val(that.attr('placeholder'));
            } else {
                that.removeClass('input-placeholder');
            }
        });
}
*/
//like select 
var wSelect = W('.select');
W('.active', wSelect).on('click', function (e) {
    var option = W('.option', e.target.parentNode);
    option.show();
});
W('.option li', wSelect).on('click', function (e) {
    var val = W(this).attr('data-value'),
        slt = this.parentNode.parentNode;
    W('.active', slt).html(val);
    W('input', slt).val(val);
    W('.option', slt).hide();
	wSelect.selectFn(this);
    //console.log(slt);
});
W('.option li', wSelect).forEach(function (item, index) {
    item.setAttribute('data-index', index);
});

// button 
W('.bt-submit-0,.bt-submit-1')
    .on('mouseenter', function (e) {
        var clsname = this.className.split(' ')[0].trim();
        W(this).addClass(clsname + '-hover');
    })
    .on('mouseleave', function (e) {
        var clsname = this.className.split(' ')[0].trim();
        W(this).removeClass(clsname + '-hover');
    })
    .on('click', function (e) {
        e.preventDefault();
    })
    .on('mousedown', function (e) {
        var clsname = this.className.split(' ')[0].trim();
        W(this).addClass(clsname + '-active');
    })
    .on('mouseup', function (e) {
        var clsname = this.className.split(' ')[0].trim();
        W(this).removeClass(clsname + '-active');
    });

// help toggle
W('.mod-help .hd').on('click', function (e) {
    var item = this.parentNode,
        ico = W('.ico', item);
    if (ico.hasClass('ico-open')) {
        ico.replaceClass('ico-open', 'ico-close');
        W(item).addClass('m-h-item-open');
    } else {
        ico.replaceClass('ico-close', 'ico-open');
        W(item).removeClass('m-h-item-open');
    }
});

//Textarea text number
(function () {
    var n = W('#text-number') , max = n.html() - 0 , timeId = 0 , textarea = W('#text-value');
    if (!max) {
        return;
    }
    textarea.on('focus', function (e) {
        handler();
    });
    textarea.on('blur', function (e) {
        clearTimeout(timeId);
    });
    function handler() {
        var str = textarea.val();
        //var len = textarea.hasClass('input-active') || _isplaceholder ? str.length : 0;
        var len =  str.length ;
        if (len > max) {
            textarea.val(str.substr(0, max));
            n.html(0);
        } else {
            n.html(max - len);
        }
        var arg = arguments;
        timeId = setTimeout(function () {
            arg.callee();
        }, 50);
    }

    handler(textarea);
    clearTimeout(timeId);
})();

(function () {
    var trigger = W('#w-fw-trigger') , box = W('.w-fw-box') , closeStr = trigger.html() , openStr = trigger.attr('data-str') , flag = true;
    if (closeStr) {
        trigger.on('click', function (e) {
            if (flag) {
                flag = false;
                trigger.html(openStr);
                box.show();
            } else {
                flag = true;
                trigger.html(closeStr);
                box.hide();
            }
            e.stopPropagation();
            e.preventDefault();
        });
    }
})();

(function () {
    var trim = QW.StringH.trim;
    var form = W('form');
    var Valid = function (form, arr) {
        var fnArray = [];
        QW.ArrayH.forEach(arr, function (item) {
            var input = W(form.query('[data-name=' + item['name'] + ']')), msgBox = input.parentNode('.i').query('div span') , html = msgBox.html();
            if (!input[0]) {
                return;
            }
            input.attr('data-html', html);
            var fn = function () {
                var val = trim(input.val());
                if (!item.callback || (item.callback && item.callback.call(input))) {
                    if (item.requireMsg && (val === '' || val === input.attr('placeholder'))) {
                        msgBox.html('<b>' + item.requireMsg + '</b>');
                        return false;
                    }
                    if (item.reg && !item.reg.test(val)) {
                        msgBox.html('<b>' + item.regMsg + '</b>');
                        return false;
                    }
                }
                msgBox.html(html);
                if (item.ok) {
                    item.ok.call(input);
                }
                return true;
            };
            fnArray.push(fn);
            input.on('blur', function () {
                fn();
            });
        });
        return fnArray;
    };
    var fnArray = Valid(form, [
        {
            name:'mail',
            requireMsg:'请输入您的邮箱',
            reg:/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/,
            regMsg:'邮箱格式错误，请重新输入'
        },
        {
            name:'checkcode',
            requireMsg:'请正确填写验证码'
        },
        {
            name:'description',
            requireMsg:'详细描述不能为空',
            ok:function () {
                try {
                    this.fire('input');
                } catch (e) {
                }
            },
            callback:function () {
                var input = this;
                input.on('focus', function () {
                    input.un('focus', arguments.callee);
                    input.parentNode('.i').query('div span').html(input.attr('data-html'));
                    try {
                        input.fire('input');
                    } catch (e) {
                    }
                });
                return true;
            }
        },
        {
            name:'uri',
            requireMsg:'快照地址不能为空'
        },
        {
            name:'url',
            requireMsg:'请输入您的网址'
        },
        {
            name:'name',
            requireMsg:'请输入您的网站名称'
        },
        {
            name:'phototype',
            requireMsg:'举报原因不能为空'
        },
        {
            name:'title',
            requireMsg:'请输入您的标题'
        },
        {
            name:'imgurl',
            requireMsg:'图片地址不可以为空'
        },
        {
            name:'icp',
            callback:function () {
                var select = W('#select-type');
                console.log(select[0]);
                if (!select[0] || select.val() < 5) {
                    return true
                }
                return false;
            },
            requireMsg:'网站ICP备案信息不能为空'
        }
    ]);
    W('#select-type').on('change', function () {
        var fn = fnArray[fnArray.length - 1];
        fn();
    });
    form.on('submit', function () {
        var flag = true;
        QW.ArrayH.forEach(fnArray, function (fn) {
            var ret = fn();
            if (!ret) {
                flag = ret;
            }
        });
        return flag;
    });
})();
function diffcheckcode(){
	var isok = false,
		checkcode = $("#checkcode").val();
	if(checkcode.length != 4){
		$('#checkcode ~ div').css('color', 'red').html("请正确填写验证码");
		isok = false;
	}else{
		$.ajax({
			url: diffcodeurl,
			dataType: 'json',
			data: {checkcode:checkcode},
			type: 'post',
			async : false,
			success: function(data){
				if(data.status == 0){
					$('#checkcode ~ div').css('color', 'red').html("请正确填写验证码");
					isok = false;
				}else{
					$('#checkcode ~ div').html('');
					isok = true;
				}
			}
		});
	}
	return isok;
}
